package controller;

import com.vandorenmes.argus.R;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import model.CaptureHandler;
import model.Machine;
import model.CaptureHandler.OnDecodedCallback;

public class CaptureController implements OnDecodedCallback
{
	private Activity scannerActivity;
	
	private CaptureHandler captureHandler;
	
	public CaptureHandler getCaptureHandler() {
		return captureHandler;
	}
	
	
	
	public CaptureController(Activity scannerActivity)
	{
		this.scannerActivity = scannerActivity;
		
		this.captureHandler = new CaptureHandler(this.scannerActivity, this);		
	}




	@Override
    public void onDecoded(String decodedData) 
    {
    	Log.v("QR", decodedData);
        ((TextView) scannerActivity.findViewById(R.id.lblMultiQR)).setText(decodedData);
    }

	@Override
	public void onLoading() 
	{
		scannerActivity.findViewById(R.id.loader).setVisibility(View.VISIBLE);
	}

	public void onWcfResponse(String decodedData) 
	{
		scannerActivity.findViewById(R.id.loader).setVisibility(View.GONE);
		TextView lbl = ((TextView) scannerActivity.findViewById(R.id.lblMultiQR));
		
		lbl.setText(decodedData);
		
		boolean decodedDataIsEmpty = decodedData.equals("");
		
		if(!decodedDataIsEmpty)
		{
//			lbl.setText(decodedData);
			scannerActivity.findViewById(R.id.machineView).setVisibility(View.GONE);
			return;
		}
//		else
//		{
//			lbl.setText("");
//		}
		
		App app = (App) scannerActivity.getApplication();	
		Machine machine = app.getCurrentMachine();
		
		Log.v("wcf", String.format("QR-code: %s", app.getCurrentQR()));
		
		if(machine != null)
		{
			fillLabelWithMachineInfo(machine);
		}
		else 
		{
			lbl.setText("ID unknown");
			scannerActivity.findViewById(R.id.machineView).setVisibility(View.GONE);
		}
	}
	
	private void fillLabelWithMachineInfo(Machine machine)
	{
		Log.v("wcf", "machine: " + machine.toString());
		((TextView) scannerActivity.findViewById(R.id.id_val)).setText(machine.getID());
		((TextView) scannerActivity.findViewById(R.id.name_val)).setText(machine.getName());
		((TextView) scannerActivity.findViewById(R.id.part_val)).setText(machine.getPart());
		((TextView) scannerActivity.findViewById(R.id.company_val)).setText(machine.getCompany());
		((TextView) scannerActivity.findViewById(R.id.desc_val)).setText(machine.getDescription());
		((TextView) scannerActivity.findViewById(R.id.status_val)).setText(machine.getStatus());			
		
		scannerActivity.findViewById(R.id.machineView).setVisibility(View.VISIBLE);
	}

	

}
